package com.mumulx.check.web.config.security.auth;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import java.security.MessageDigest;

/*
 *  Created by IntelliJ IDEA.
 *  User: 木木
 *  Date: 2020/10/25
 *  Time: 22:30
 *  加密算法
 */
//@Component
@Slf4j
public class MyPasswordEncoder implements PasswordEncoder {
    private static final String SALT = "jzd,.,.";
    /**
     * 加密
     * @param charSequence 需要加密的密码
     * @return
     */
    @Override
    public String encode(CharSequence charSequence) {
        log.info("**********密码校验"+charSequence.toString());
        //charSequence = charSequence+SALT;
        byte[] byteArray = charSequence.toString().getBytes();
        //使用md5进行加密
        return DigestUtils.md5Hex(byteArray);
    }

    /**
     * 判断加密后的密码是否一致
     * @param charSequence 需要加密的密码
     * @param password    数据库中加密后的密码，权限框架会直接传入
     * @return
     */
    @Override
    public boolean matches(CharSequence charSequence, String password) {
        String pwd=encode(charSequence);
        if(pwd.equals(password)){
            return true;
        }
        return false;
    }

    /*public static void main(String[] args) {
        MyPasswordEncoder passwordEncoder=new MyPasswordEncoder();
        System.out.println(passwordEncoder.encode("14752109608"));
    }*/
}